package ph.com.gs3.formalistics.presenter.notification;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.presenter.MainActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

public class ServiceEventNotifierUtility {

	public static final String TAG = ServiceEventNotifierUtility.class.getSimpleName();

	public static final int NOTIFICATION_ID_PRIMARY = 1;
	public static final int NOTIFICATION_ID_SECONDARY = 2;

	public static final Class<?> DEFAULT_DESTINATION_ACTIVITY = MainActivity.class;
	public static final String DEFAULT_NOTIFICATION_TITLE = "Formalistics";

	public static final int DEFAULT_RESULT_CODE = 0;

	// ==============================================================================
	// {{ Default Instance

	private static ServiceEventNotifierUtility defaultInstance;

	public static ServiceEventNotifierUtility getDefaultInstance() {
		if (defaultInstance == null) {
			defaultInstance = new ServiceEventNotifierUtility();
		}

		return defaultInstance;
	}

	// }}

	public void displayBasicNotification(Context context, String message) {
		displayBasicNotification(context, NOTIFICATION_ID_PRIMARY, message);
	}

	public void displayBasicNotification(Context context, int notificationId, String message) {

		NotificationCompat.Builder builder = getBasicNotificationBuilder(context, message, null);
		displayNotificationUsingBuilder(context, notificationId, builder);

	}

	public void displayBasicNotification(Context context, int notificationId, String message,
	        Bundle extras) {

		NotificationCompat.Builder builder = getBasicNotificationBuilder(context, message, extras);
		displayNotificationUsingBuilder(context, notificationId, builder);

	}

	public void displayNotificationWithProgress(Context context, int notificationId,
	        String message, int max, int progress, boolean indeterminate, Bundle extras) {

		NotificationCompat.Builder builder = getBasicNotificationBuilder(context, message, extras);
		builder.setProgress(max, progress, indeterminate);
		displayNotificationUsingBuilder(context, notificationId, builder);

	}

	public void dismissPrimaryNotification(Context context) {
		dismissNotification(context, NOTIFICATION_ID_PRIMARY);
	}

	public void dismissNotification(Context context, int notificationId) {
		NotificationManager notificationManager = (NotificationManager) context
		        .getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.cancel(notificationId);
	}

	private NotificationCompat.Builder getBasicNotificationBuilder(Context context, String message,
	        Bundle extras) {
		NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

		// basic configuration
		builder.setAutoCancel(true);
		builder.setContentTitle("Formalistics");
		builder.setContentText(message);
		builder.setSmallIcon(R.drawable.ic_launcher);

		// Action intent after clicking the notification
		Intent resultIntentNotification = new Intent(context, DEFAULT_DESTINATION_ACTIVITY);
		if (extras != null) {
			resultIntentNotification.putExtras(extras);
		}

		// TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
		//
		// // Adds the back stack for the Intent (but not the Intent itself)
		// stackBuilder.addParentStack(DEFAULT_DESTINATION_ACTIVITY);
		// // Adds the Intent that starts the Activity to the top of the stack
		// stackBuilder.addNextIntent(resultIntentNotification);
		//
		// PendingIntent resultPendingIntentNotification =
		// stackBuilder.getPendingIntent(0,
		// PendingIntent.FLAG_UPDATE_CURRENT);

		PendingIntent resultPendingIntentNotification = PendingIntent.getActivity(context,
		        DEFAULT_RESULT_CODE, resultIntentNotification, PendingIntent.FLAG_UPDATE_CURRENT);

		builder.setContentIntent(resultPendingIntentNotification);

		return builder;
	}

	private void displayNotificationUsingBuilder(Context context, int notificationId,
	        NotificationCompat.Builder builder) {
		// display the notification
		Notification notification = builder.build();
		NotificationManager manager = (NotificationManager) context
		        .getSystemService(Context.NOTIFICATION_SERVICE);
		manager.notify(notificationId, notification);
	}
}
